package com.ymatou.api.marketingseller.testcase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.api.marketingseller.service.CreateSellerPromotionCall;
import com.ymatou.api.marketingseller.service.GetPromotionByIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.SellerPromotionCallService;
import com.ymttest.database.model.YmtSellerPromotion;
import com.ymttest.database.sqlwapper.YmtSellerPromotionIMapper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

/**
 * @since 2016-12-13 23:16:14
 * @author chencan
 *
 */
public class Ts_GetPromotionById {
	private static GetPromotionByIdCall getPromotionByIdCall;
	
	/** 响应报文返回的code */
	private static int rCode = 200;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("业务说明：根据促销活动ID获取促销活动详情");
	}

	@Before
	public void caseUp() {
		getPromotionByIdCall = new GetPromotionByIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getPromotionByIdCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionById_001(){
		Logger.start(true, "根据促销活动ID获取促销活动详情");
		try{
			// 根据sellerid查询第一条活动记录
			List<YmtSellerPromotion> sellerPromotionIDlist = new YmtSellerPromotionIMapper().selectPromotionByPromotionState(20323743);
			YmtSellerPromotion promotionstate =sellerPromotionIDlist.get(0);
			int sellerId = promotionstate.getSellerId();
			int promotionId = promotionstate.getPromotionId();
			
			// 请求参数
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("SellerId",sellerId);
			map.put("PromotionId",promotionId);
			JSONObject updateMap = new GetPromotionByIdCall().callAndGetReturnData(map);
			
			//获取返回报文结果
			JSONObject PromotionInfo = updateMap.getJSONObject("Data").getJSONObject("PromotionInfo");
			int code = updateMap.getInt("Code");      //Code是否等于200
 			int  promotion = PromotionInfo.getInt("PromotionId"); // 促销商品ID
			int seller = PromotionInfo.getInt("SellerId");        // 买手ID
			int promotiontype =PromotionInfo.getInt("PromotionType");
			
			// 查询SQL促销数据
			List<YmtSellerPromotion> sellerPromotionList = new YmtSellerPromotionIMapper().selectPromotionByPromotionId(promotion);
			YmtSellerPromotion ymtSellerPromotion = sellerPromotionList.get(0);
			int p = ymtSellerPromotion.getPromotionId();
			int s = ymtSellerPromotion.getSellerId();
			int t = ymtSellerPromotion.getPromotionType();
			
			//断言
			Logger.verifyEquals(rCode, code, "响应Code是否等于200");
			Logger.verifyEquals(p, promotion, "promotionId是否等于");
			Logger.verifyEquals(s, seller, "买手ID");
			Logger.verifyNotNull(PromotionInfo, "返回结果不为空");
			Logger.verifyIsContains(t, promotiontype, "促销活动状态");
			
			
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionById_0011(){
		Logger.start(true, "根据促销活动ID获取促销活动详情");
		try{
 			int sellerId = 20323743;
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			
			// 创建活动
			Map<String,Object> updateMap = new HashMap<String,Object>();
			updateMap.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(0));
			updateMap.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(updateMap);
			
			// 获取新促销活动信息
			int promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
			JSONObject jsonObject = new GetPromotionByIdCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"promotionId", promotionId));
			
			// 获取返回报文结果
			JSONObject promotionInfo = jsonObject.getJSONObject("Data").getJSONObject("PromotionInfo");
			int code = jsonObject.getInt("Code");
			int seller = promotionInfo.getInt("SellerId");
			
			// 断言
			Logger.verifyEquals(rCode,code, "响应报文code是否等于200");
			Logger.verifyEquals(sellerId, seller, "买手ID是否相等");
			Logger.verifyNotNull(promotionInfo, "返回结果不为空");
			
		}catch (Exception e) {
			Logger.fail(e);
			e.printStackTrace();
		}
	}


	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionById_002(){
		Logger.start(true, "活动不存在");
		try{

 			int sellerId = 20323743;
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			
			// 创建活动
			Map<String,Object> updateMap = new HashMap<String,Object>();
			updateMap.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(0));
			updateMap.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(updateMap);
			
			// 获取新促销活动信息
			int promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
			JSONObject jsonObject = new GetPromotionByIdCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"promotionId", 5155537));
			
			// 获取返回报文结果
			String retBean = jsonObject.getJSONObject("Data").getString("PromotionInfo");
			int code = retBea.getInt("Code");  //Code是否等于200
			
			//断言
			Logger.verifyEquals(rCode, code, "响应Code是否等于200");
			Logger.verifyIsNull(retBean, "返回结果为null");
			
		}catch (Exception e) {
			Logger.fail(e);
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionById_003(){
		Logger.start(true, "买手和活动ID不匹配");
		try{
 			int sellerId = 20323743;
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			
			// 创建活动
			Map<String,Object> updateMap = new HashMap<String,Object>();
			updateMap.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(0));
			updateMap.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(updateMap);
			
			// 获取新促销活动信息
			int promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
			JSONObject jsonObject = new GetPromotionByIdCall().callAndGetReturnData(MapUtil.hashMap("sellerId", 3383,"promotionId", promotionId));
			
			// 获取返回报文结果
			String promotionInfo = jsonObject.getJSONObject("Data").getString("PromotionInfo");
			int code = jsonObject.getInt("Code");

			//断言
			Logger.verifyEquals(rCode, code, "响应Code是否等于200");
			Logger.verifyIsNull(promotionInfo, "返回结果为null");
			
		}catch (Exception e) {
			Logger.fail(e);
			e.printStackTrace();
		}
	}
	
}
